<?php
namespace Application\Social;

use Doctrine\ORM\EntityManager;
use Application\Entity\User;
use DateTime;
use ReverseOAuth2\AbstractOAuth2Client;
use Application\Component\AbstractComponent;
use ReverseOAuth2\Client\Facebook;
use \Zend\Http\PhpEnvironment\Request;

/**
 * UserServcie
 */
class FacebookComponent extends Facebook
{

    public function getFriends($after,$limit=100)
    {
        if (isset($this->session->token->access_token)) {
            $urlFiends = $this->options->getFriendsUri() . '?limit='.$limit;
            if(isset($after))
            {
                $urlFiends.='&__after_id='.$after;
            }
            $urlFiends.='&access_token='. $this->session->token->access_token;
            $client = $this->getHttpclient()
                ->resetParameters(true)
                ->setHeaders(array(
                'Accept-encoding' => 'deflate, compress'
            ))
                ->setMethod(Request::METHOD_GET)
                ->setUri($urlFiends);
            
            $retVal = $client->send()->getContent();
            
            if (strlen(trim($retVal)) > 0) {
                
                $friends = \Zend\Json\Decoder::decode($retVal);
                return $friends;
            } else {
                
                $this->error = array(
                    'internal-error' => 'Get info return value is empty.'
                );
                return false;
    
    		}
    
    	} else {
    
    		$this->error = array('internal-error' => 'Session access token not found.');
    		return false;
    
    	}
    }
    
    public function getAlbums($after,$limit=100)
    {
         $fields = "&fields=id,name,cover_photo,count";
        
    	if (isset($this->session->token->access_token)) {
    		$url = $this->options->getAlbumsUri() . '?limit='.$limit;
    		$url.=$fields;
    		if(isset($after))
    		{
    			//$url.='&__after_id='.$after;
    			$url.='&after='.$after;
    		}
    		$url.='&access_token='. $this->session->token->access_token;
    		$client = $this->getHttpclient()
    		->resetParameters(true)
    		->setHeaders(array(
    				'Accept-encoding' => 'deflate, compress'
    		))
    		->setMethod(Request::METHOD_GET)
    		->setUri($url);

    		var_dump($after);
    		var_dump($url);
    		$retVal = $client->send()->getContent();
    
    		if (strlen(trim($retVal)) > 0) {
    
    			$data = \Zend\Json\Decoder::decode($retVal);
    			return $data;
    		} else {
    
    			$this->error = array(
    					'internal-error' => 'Get info return value is empty.'
    			);
    			return false;
    
    		}
    
    	} else {
    
    		$this->error = array('internal-error' => 'Session access token not found.');
    		return false;
    
    	}
    }
    
    public function getLikes($after,$limit=100)
    {
    
    	if (isset($this->session->token->access_token)) {
    		$url = $this->options->getLikesUri() . '?limit='.$limit;

    		if(isset($after))
    		{
    			//$url.='&__after_id='.$after;
    			$url.='&after='.$after;
    		}
    		$url.='&access_token='. $this->session->token->access_token;
    		$client = $this->getHttpclient()
    		->resetParameters(true)
    		->setHeaders(array(
    				'Accept-encoding' => 'deflate, compress'
    		))
    		->setMethod(Request::METHOD_GET)
    		->setUri($url);
    
    		$retVal = $client->send()->getContent();
    
    		if (strlen(trim($retVal)) > 0) {
    
    			$data = \Zend\Json\Decoder::decode($retVal);
    			return $data;
    		} else {
    
    			$this->error = array(
    					'internal-error' => 'Get info return value is empty.'
    			);
    			return false;
    
    		}
    
    	} else {
    
    		$this->error = array('internal-error' => 'Session access token not found.');
    		return false;
    
    	}
    }
    
    public function getLikeAlbums($like,$after,$limit=100)
    {
    
    	if (isset($this->session->token->access_token)) {
    		$url = $this->options->getLikesUri() .$like.'/albums?limit='.$limit;
    
    		if(isset($after))
    		{
    			$url.='&__after_id='.$after;
    		}
    		$url.='&access_token='. $this->session->token->access_token;
    		$client = $this->getHttpclient()
    		->resetParameters(true)
    		->setHeaders(array(
    				'Accept-encoding' => 'deflate, compress'
    		))
    		->setMethod(Request::METHOD_GET)
    		->setUri($url);
    
    		$retVal = $client->send()->getContent();
    
    		if (strlen(trim($retVal)) > 0) {
    
    			$data = \Zend\Json\Decoder::decode($retVal);
    			return $data;
    		} else {
    
    			$this->error = array(
    					'internal-error' => 'Get info return value is empty.'
    			);
    			return false;
    
    		}
    
    	} else {
    
    		$this->error = array('internal-error' => 'Session access token not found.');
    		return false;
    
    	}
    }
    
    public function getPhotos($album,$after,$limit=100)
    {
    	
    	if (isset($this->session->token->access_token)) {
    		$url = $this->options->getAlbumPhotosUri() .$album.'/photos?limit='.$limit;
    		if(isset($after))
    		{
    			//$url.='&__after_id='.$after;
    			$url.='&after='.$after;
    		}
    		$url.='&access_token='. $this->session->token->access_token;
    		$client = $this->getHttpclient()
    		->resetParameters(true)
    		->setHeaders(array(
    				'Accept-encoding' => 'deflate, compress'
    		))
    		->setMethod(Request::METHOD_GET)
    		->setUri($url);
    		var_dump($url);
    
    		$retVal = $client->send()->getContent();
    
    		if (strlen(trim($retVal)) > 0) {
    
    			$data = \Zend\Json\Decoder::decode($retVal);
    			return $data;
    		} else {
    
    			$this->error = array(
    					'internal-error' => 'Get info return value is empty.'
    			);
    			return false;
    
    		}
    
    	} else {
    
    		$this->error = array('internal-error' => 'Session access token not found.');
    		return false;
    
    	}
    }
    
    public function getVideos($album,$after,$limit=100)
    {
    	
    	if (isset($this->session->token->access_token)) {
    		$url = $this->options->getAlbumPhotosUri() .$album.'/videos?limit='.$limit;
    		if(isset($after))
    		{
    			//$url.='&__after_id='.$after;
    			$url.='&after='.$after;
    		}
    		$url.='&access_token='. $this->session->token->access_token;
    		$client = $this->getHttpclient()
    		->resetParameters(true)
    		->setHeaders(array(
    				'Accept-encoding' => 'deflate, compress'
    		))
    		->setMethod(Request::METHOD_GET)
    		->setUri($url);
    		var_dump($url);
    
    		$retVal = $client->send()->getContent();
    
    		if (strlen(trim($retVal)) > 0) {
    
    			$data = \Zend\Json\Decoder::decode($retVal);
    			return $data;
    		} else {
    
    			$this->error = array(
    					'internal-error' => 'Get info return value is empty.'
    			);
    			return false;
    
    		}
    
    	} else {
    
    		$this->error = array('internal-error' => 'Session access token not found.');
    		return false;
    
    	}
    }
}
